package com.lfy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lfy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
public class FlowLimitController {
    @Autowired
    private UserService userService;

    @GetMapping("/testA")
    public String testA() {
        return userService.getUserName();
    }
 
    @GetMapping("/testB")
    public String testB() {
        return userService.getUserName();
    }

    //测试异常比例
    @GetMapping("/testC")
    public String testC(){
        int a=10/0;
        return "------testC";
    }

    @GetMapping("/testD")
    public String testD(){
        try {
            //暂停几秒钟线程
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) { e.printStackTrace();
        }
        return "------testD";
    }

    /**
     * 测试热点key接口
     * @param p1
     * @param p2
     * @return
     */
    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKey",blockHandler = "dealHandler_testHotKey")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2){
        return "------testHotKey";
    }

    /**
     * 热点key报错兜底方法
     * 必须要加exception参数，否则兜底方法不生效
     * @param p1
     * @param p2
     * @param exception
     * @return
     */
    public String dealHandler_testHotKey(String p1, String p2, BlockException exception)
    {
        return "-----dealHandler_testHotKey";
    }
}